// pages/wxbangding/wxbangding.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPhone: '',
    randID: '',

  },

  toast(img) {
    wx.showToast({
      title: img,
      icon: 'none',
      duration: 2000
    })
  },

  // 获取输入账号
  // 给账号的input加上bindinput="usernameInput"，赋值
  userPhoneInput: function (e) {
    this.setData({
      userPhone: e.detail.value
    })
    // console.log(this.data.userPhone)
  },

  // 获取输入验证码
  // 给验证码的input加上bindinput="randIDInput"，赋值
  randIDInput: function (e) {
    this.setData({
      randID: e.detail.value
    })
    // console.log(this.data.randID)
  },

  tologin(e) {
    var userPhone = this.data.userPhone
    var randID = this.data.randID
    console.log(userPhone + randID)
    var phoneReg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
    if (!phoneReg.test(userPhone)) {
      this.toast('请输入正确手机号!');
      return false
    }
    if (!randID) {
      this.toast('请输入验证码!')
      return false;
    }
    this.register()  
  },

  register: function () {
    console.log("register- userPhone: " + this.data.userPhone + " randID: " + this.data.randID)
    var domain = getApp().globalData.myHttp
    var url = domain + '/user/threeWayLogin'
    var opt = {
      phone: this.data.userPhone,
      randID: this.data.randID,
      openId:wx.getStorageSync("openID"),
      nickname: wx.getStorageSync("wxUserInfo").nickName,
      sex: wx.getStorageSync("wxUserInfo").gender,
      headimgurl: wx.getStorageSync("wxUserInfo").avatarUrl,
      age:'',
      loginType:'weixin'
    }
    console.log(JSON.stringify(opt))
    wx.request({
      url: url,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: opt,
      success: function (res) {
        console.log(res.data)
        var data = res.data
        if (data.success == true) {
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          })
          wx.showLoading({
            title: '登录中',
          })
          wx.request({
            url: getApp().globalData.myHttp + '/user/threeWayLoginVal',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
              "openId": wx.getStorageSync("openID"),
              "loginType": "weixin",
            },
            success(result) {
              wx.hideLoading()
              console.log(result)
              console.log(result.data)
              console.log("用户绑定手机号后登录的结果为：" + result)
              console.log("用户绑定手机号后登录的结果为：" + JSON.stringify(result))
              //判断是否已经绑定过手机号查询数据库中是否有该用户信息
              if (result.data.success) {
                var user = result.data.obj
                console.log(user)
                // 缓存用户数据
                wx.setStorageSync('user', user)
                // 保存用户账号
                getApp().globalData.userPhone = user.userPhone;

                //登录成功
                console.log("微信登录成功")
                wx.showToast({
                  title: result.data.msg,
                  icon: 'none',
                  duration: 2000
                })
                //跳转回前三个页面
                wx.navigateBack({
                  delta: 3
                })
              } else {
                console.log(result.data.msg)
                //无该用户信息，绑定手机号
                wx.showToast({
                  title: result.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            complete(){
              console.log("用户绑定手机号后登录成功")
            }
          })
        }
        else (
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          })
        )

      }
    })
  },

  // 发送获取验证码
  clickSend: function () {
    var domain = getApp().globalData.myHttp
    var url = domain + '/user/sendMessage'
    var opt = {
      phone: this.data.userPhone,
      send: true
    }
    wx.request({
      url: url,
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      data: opt,
      success: function (res) {
        console.log(res.data)
        var randID = res.data
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("进入微信绑定手机号界面")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})